package com.nf.health.app.glide;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.nf.health.app.R;
import com.nf.health.app.utils.LogUtil;

/**
 * Created by Jason on 2016/10/12.
 * @author yyh
 */
public class GlideUtil  {
    public static void loadNetImg(Context cnt,ImageView imageView,String imgUrl,int sourseId){
        Glide.with(cnt)
                .load(imgUrl)
                .placeholder(sourseId)
                .into(imageView);

    }

    public static void loadNetImgC(Context cnt,ImageView imageView,String imgUrl,int sourseId){
        Glide.with(cnt)
                .load(imgUrl)
                .asBitmap()
                .placeholder(sourseId)
                .centerCrop()
                .into(imageView);

    }

    /**
     * 这种方式可以让glide一定加载出来图片
     * 设置图片不加入到磁盘缓存
     * 不进行缓存
     * @param cnt
     * @param imageView
     * @param imgUrl
     * @param sourseId
     */
    public static void loadImgUrl(Context cnt,ImageView imageView,String imgUrl,int sourseId){
        Glide.with(cnt)
                .load(imgUrl)
                .asBitmap()
                .placeholder(sourseId)
                .centerCrop()
                .skipMemoryCache( true )
                .diskCacheStrategy( DiskCacheStrategy.NONE )
                .into(imageView);

    }
    /**
     * 解决部分图片加载不出来的问题
     *http://abv.cn/music/光辉岁月.mp3
     * @param context
     * @param imageView
     * @param imgUrl
     * @param sourseId
     */
    public static void loadNetImg2(Context context, final ImageView imageView,String imgUrl, final int sourseId){
        Glide.with(context).load(imgUrl).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                imageView.setImageBitmap(resource);
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);
                imageView.setBackgroundResource(sourseId);
            }
        }); //方法中设置asBitmap可以设置回调类型
    }

    /**
     * 加载本地的图片
     * @param cnt
     * @param imageView
     * @param sourseId
     */
    public static void loadLocalImg(Context cnt,ImageView imageView,int sourseId){
        Glide.with(cnt).load(sourseId).into(imageView);
    }

    /**
     * 加载本地的图片
     * @param cnt
     * @param imageView
     * @param sourseId
     */
    public static void loadLocalImgC(Context cnt,ImageView imageView,int sourseId){
        Glide.with(cnt).load(sourseId).asBitmap().centerCrop().into(imageView);
    }
}
